package com.zry.ai.repository;

import org.springframework.stereotype.Component;

import java.awt.image.Kernel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Component
public class InMemoryChatHistoryRepository implements ChatHistoryRepository{
    private final Map<String,List<String>> ChatHistory=new HashMap<>();
    @Override
    public void save(String type, String chatId) {
     if(!ChatHistory.containsKey(type)){
         ChatHistory.put(type,new ArrayList<>());
     }
     List<String> chatIds = ChatHistory.get(type);
       // List<String> chatIds=ChatHistory.computeIfPresent(type, k -> new ArrayList<>());
        if(chatIds.contains(chatId)){
            return;
        }
        chatIds.add(chatId);
    }

    @Override
    public List<String> getChatIds(String type) {
//        List<String> chatIds=ChatHistory.get(type);
//        return chatIds==null ? List.of() : chatIds;

        //简化写法如下:
        return ChatHistory.getOrDefault(type,List.of());
    }
}
